/*
 * @Author: your name
 * @Date: 2021-04-14 10:54:33
 * @LastEditTime: 2021-04-24 10:50:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /flutter_demo/lib/pages/Forms.dart
 */
import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class FormsPage extends StatefulWidget {
  String title;
  FormsPage({this.title = '表单'});

  @override
  _FormsPageState createState() => _FormsPageState();
}

class _FormsPageState extends State<FormsPage> {
  // TextEditingController用于初始化赋值
  // var _username = TextEditingController();
  String _password;
  String _username;
  bool _flag = false;
  int _radioCheck = 1;
  bool _switch = false;
  DateTime _nowDate = DateTime.now();
  // var _nowTime = TimeOfDay(hour: 12, minute: 20);

  @override
  void initState() {
    super.initState();
    // print('当前时间$now');
    // print(now.millisecondsSinceEpoch);
    // print(DateTime.fromMillisecondsSinceEpoch(1618543546067));
    // print(formatDate(DateTime.now(), ['yyyy', '-', 'mm', '-', 'dd']));
  }

  // List<Widget> _checkBoxList() {
  //   List<Widget> tempList = [];
  //   for (var i = 0; i < this._checkList.length; i++) {
  //     tempList.add(
  //       Row(
  //         children: <Widget>[
  //           Text(this.checkList[i]['title'] + ':'),
  //           CehckBox(
  //             value: this.checkList[i]['title'],
  //             onChanged: (value) {
  //               setState(() {
  //                 this.checkList[i]['checked'] = value
  //               });
  //             }
  //           )
  //         ],
  //       )
  //     );
  //   }
  //   return tempList;
  // }

  // _showDatePicker() async {
  //   var result = await showDatePicker(
  //     context: context,
  //     initialDate: _nowDate,
  //     firstDate: DateTime(1980),
  //     lastDate: DateTime(2100),
  //     // locale: Locale('zh')
  //   );
  //   print(result);
  //   setState(() {
  //     _nowDate = result;
  //   });
  // }
  _showDatePicker() {
    DatePicker.showDatePicker(
      context,
      onMonthChangeStartWithFirstDate: true,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确定', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.red)),
      ),
      minDateTime: DateTime.parse('1980-05-12'),
      maxDateTime: DateTime.parse('2100-05-12'),
      initialDateTime: _nowDate,
      dateFormat: 'yy年M月d日    EEE,H时:m分',
      pickerMode: DateTimePickerMode.datetime,
      // dateFormat: 'yyyy-MMMM-dd',
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      // onChange: (dateTime, List<int> index) {
      //   setState(() {
      //     _nowDate = dateTime;
      //   });
      // },
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _nowDate = dateTime;
        });
      },
    );
  }

  // _showTimePicker() async {
  //   var result = await showTimePicker(context: context, initialTime: _nowTime);
  //   print(result);
  //   setState(() {
  //     _nowTime = result;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          child: Text('返回'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: SingleChildScrollView(
            child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            children: <Widget>[
              TextField(
                // maxLines: 4,
                // obscureText: true,
                // controller: _username,
                decoration: InputDecoration(
                    // hintText: '请输入内容',
                    border: OutlineInputBorder(),
                    labelText: '用户名'),
                onChanged: (value) {
                  setState(() {
                    _username = value;
                  });
                },
              ),
              SizedBox(height: 20),
              TextField(
                // maxLines: 4,
                obscureText: true,
                decoration: InputDecoration(
                    // hintText: '请输入密码',
                    // icon: Icon(Icons.lock),
                    border: OutlineInputBorder(),
                    labelText: '密码'),
                onChanged: (value) {
                  setState(() {
                    _password = value;
                  });
                },
              ),
              SizedBox(height: 20),
              Checkbox(
                  value: _flag,
                  onChanged: (value) {
                    setState(() {
                      _flag = value;
                    });
                  }),
              SizedBox(height: 20),
              Row(
                children: <Widget>[
                  Text('男'),
                  Radio(
                    value: 1,
                    onChanged: (value) {
                      setState(() {
                        _radioCheck = value;
                      });
                    },
                    groupValue: _radioCheck,
                  ),
                  SizedBox(height: 20),
                  Text('女'),
                  Radio(
                    value: 2,
                    onChanged: (value) {
                      setState(() {
                        _radioCheck = value;
                      });
                    },
                    groupValue: _radioCheck,
                  ),
                ],
              ),
              SizedBox(height: 20),
              Switch(
                  value: _switch,
                  onChanged: (value) {
                    setState(() {
                      _switch = value;
                    });
                  }),
              SizedBox(height: 20),
              Column(
                children: <Widget>[
                  InkWell(
                    child: Row(
                      children: <Widget>[
                        Text('${formatDate(_nowDate, [
                          "yyyy",
                          "-",
                          "mm",
                          "-",
                          "dd",
                          ' ',
                          HH,
                          ':',
                          nn
                        ])}'),
                        Icon(Icons.arrow_drop_down)
                      ],
                    ),
                    onTap: _showDatePicker,
                  )
                ],
              ),
              // Column(
              //   mainAxisAlignment: MainAxisAlignment.center,
              //   children: <Widget>[
              //     InkWell(
              //       child: Row(
              //         children: <Widget>[
              //           Text('${formatDate(_nowDate, [
              //             "yyyy",
              //             "-",
              //             "mm",
              //             "-",
              //             "dd"
              //           ])}'),
              //           Icon(Icons.arrow_drop_down)
              //         ],
              //       ),
              //       onTap: () {
              //         _showDatePicker();
              //       },
              //     ),
              //     InkWell(
              //       child: Row(
              //         children: <Widget>[
              //           Text('${_nowTime.format(context)}'),
              //           Icon(Icons.arrow_drop_down)
              //         ],
              //       ),
              //       onTap: () {
              //         _showTimePicker();
              //       },
              //     ),
              //   ],
              // ),
              SizedBox(height: 20),
              Container(
                width: double.infinity,
                height: 40,
                child: ElevatedButton(
                  child: Text('登录'),
                  onPressed: () {
                    print({
                      this._username,
                      this._password,
                      this._flag,
                      this._radioCheck,
                      this._switch
                    });
                  },
                ),
              ),
            ],
          ),
        )));
  }
}
